package com.atguigu.gmall.search.api;

import com.atguigu.gmall.common.result.Result;
import com.atguigu.gmall.model.list.Goods;
import com.atguigu.gmall.model.list.SearchParam;
import com.atguigu.gmall.model.list.SearchResponseVo;
import com.atguigu.gmall.search.dao.GoodsEsRepository;
import com.atguigu.gmall.search.service.SkuEsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;

/**
 * @author dyqstart
 * @create 2021-08-13-20:00
 * @since 1.0.0
 */
@RequestMapping("/es/sku/inner")
@RestController
public class SkuEsController {

    @Autowired
    SkuEsService skuEsService;

    /**
     * ant风格的路径
     * es中商品上架
     * @param goods
     * @return
     */
    @PostMapping("/onsale")
    public Result saveSkuToEs(@RequestBody Goods goods){
        skuEsService.save(goods);
        return Result.ok();
    }

    @PostMapping("/canclesale/{skuId}")
    public Result saveSkuToEs(@PathVariable("skuId") Long skuId){
        skuEsService.deleteById(skuId);
        return Result.ok();
    }

    /**
     * 检索复杂属性
     * @param searchParam
     * @return
     */
    @PostMapping("/search")
    public SearchResponseVo searchGood(@RequestBody SearchParam searchParam){
        //TODO 根据不同检索属性检索出所有的商品
        //构建出具有以下功能的dsl
        //查询、过滤、模糊检索、排序、分页、高亮、聚合

        SearchResponseVo responseVo = skuEsService.search(searchParam);
        return responseVo;

    }

    /**
     * 将指定skuId的商品更新成新的热度值
     * @param skuId
     * @param hotScore
     * @return
     */
    @GetMapping("/hotscore/{skuId}")
    public Result updateSkuHotScore(@PathVariable("skuId") Long skuId,
                                    @RequestParam("hotscore") Long hotScore){
        skuEsService.updateSkuHotScore(skuId,hotScore);

        return Result.ok();
    }

}
